/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2023-02-12
 *
 * 数据表单钩子
 */
import { useBase } from "@/hooks/base/useBase"
import { useStore } from "@/hooks/store/useStore"

export const useUpload = (dataForm, dataPreview) => {

  const { i18n } = useBase()
  const { base } = useStore()

  const upload_headers = ref({
    Authorization: 'Bearer ' + base.token
  })


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-13
   *
   * 头像上传成功函数
   *
   * @param {[type]} res 上传结果
   * @param {[type]} file 上传文件
   * @return {[type]}
   */
  const handleAvatarSuccess = (res, file) => {
    dataForm.avatar = res.data
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-13
   *
   * Logo上传成功函数
   *
   * @param {[type]} res 上传结果
   * @param {[type]} file 上传文件
   * @return {[type]}
   */
  const handleLogoSuccess = (res, file) => {
    dataForm.logo = res.data
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-13
   *
   * 图片上传成功函数
   *
   * @param {[type]} res 上传结果
   * @param {[type]} file 上传文件
   * @return {[type]}
   */
  const handlePictureSuccess = (res, file) => {
    dataForm.picture = res.data
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-13
   *
   * 营业执照上传成功函数
   *
   * @param {[type]} res 上传结果
   * @param {[type]} file 上传文件
   * @return {[type]}
   */
  const handleBusinessLicenseSuccess = (res, file) => {
    dataForm.business_license = res.data
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-13
   *
   * 数据图片上传成功函数
   *
   * @param {[type]} res 上传结果
   * @param {[type]} file 上传文件
   * @return {[type]}
   */
  const handleDataSuccess = (res, file) => {
    const title = res.data.title
    const url = res.data.url

    dataForm[title] = url

    const img = document.getElementById(title)

    img.src = url
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-13
   *
   * 图片上传验证函数
   *
   * @param {[type]} file 上传文件
   * @return {[type]}
   */
  const beforePictureUpload = (file) => {
    const isPicture = file.type === 'image/jpeg' || file.type === 'image/png'
    const isLt8M = file.size / 1024 / 1024 < 8

    if (!isPicture) {
      let message = i18n('common.picture.rules.type')
      $message.error(i18n(message))
    }
    if (!isLt8M) {
      let message = i18n('common.picture.rules.size')
      $message.error(i18n(message))
    }

    return isPicture && isLt8M
  }


  const beforeAttachmentUpload = (file) => {
    const isResource = (
      // image
      file.type === 'image/png' || file.type === 'image/jpeg' || file.type === 'image/gif' ||
      // pdf
      file.type === 'application/pdf' ||
      // xls
      file.type === 'application/vnd.ms-excel' ||
      file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' ||
      // ppt
      file.type === 'application/vnd.ms-powerpoint' ||
      file.type === 'application/vnd.openxmlformats-officedocument.presentationml.presentation' ||
      // word
      file.type === 'application/msword' ||
      file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
    );

    const isLt50M = file.size / 1024 / 1024 < 50;

    if (!isResource) {
      let message = i18n('common.file.rules.type');
      $message.error(message)
    }
    if (!isLt50M) {
      let message = i18n('common.file.rules.size');
      $message.error(message)
    }

    return isResource && isLt50M;
  }

  const handleAttachmentPreview = (file) => {
    dataPreview.url = file.url;
    dataPreview.status = true;
  }

  const handleAttachmentDownload = (file) => {
    window.location.href = file.url;
  }

  const handleAttachmentSuccess = (res, file) => {
    dataForm.url.push(res.data.url);
    dataForm.filename.push(res.data.filename);
  }

  const handleAttachmentRemove = (file, fileList) => {
    let url = []
    let filename = []
    fileList.forEach(res => {
      if(!res.filename) {
        url.push(res.url);
        filename.push(res.name);
      }
      else {
        url.push(res.url);
        filename.push(res.name);
      }
    });

    dataForm.url      = url
    dataForm.filename = filename
  }

  return {
    upload_headers,
    handleAvatarSuccess,
    handleLogoSuccess,
    handleDataSuccess,
    handlePictureSuccess,
    handleBusinessLicenseSuccess,
    beforePictureUpload,
    beforeAttachmentUpload,
    handleAttachmentPreview,
    handleAttachmentDownload,
    handleAttachmentSuccess,
    handleAttachmentRemove,
  }
}
